package com.yawn.zerodefect.service;

import com.yawn.zerodefect.controller.entity.RestData;
import com.yawn.zerodefect.controller.entity.RestResponse;
import com.yawn.zerodefect.dao.ProjectDao;
import com.yawn.zerodefect.entity.Defect;
import com.yawn.zerodefect.util.ActivitiUtil;
import com.yawn.zerodefect.util.Constant;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.identity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class HistoricalService {

    @Resource
    private HistoryService historyService;
    @Resource
    private ProjectDao projectDao;

    public RestResponse myStarted(User user, int first, int size) {
        HistoricProcessInstanceQuery hisQuery = historyService.createHistoricProcessInstanceQuery()
                .processDefinitionKey(Constant.PD_KEY_ZERO_DEFECT)
//                .finished()
                .startedBy(user.getId())
                .orderByProcessInstanceEndTime().desc();
        List<HistoricProcessInstance> historicInstanceList = hisQuery.listPage(first, size);
        List<Defect> defectList = new ArrayList<>();
        for (HistoricProcessInstance instance : historicInstanceList) {
            List<HistoricVariableInstance> varList = historyService.createHistoricVariableInstanceQuery()
                    .processInstanceId(instance.getId()).list();
            Defect defect = new Defect();
            defect.setFinished(instance.getEndTime() != null);
            ActivitiUtil.setHistoricVars(defect, varList);
            defectList.add(defect);
        }
        return RestData.newData(defectList, first, size, hisQuery.count());
    }

    public RestResponse myFinished(User user, int first, int size) {
        HistoricProcessInstanceQuery hisQuery = historyService.createHistoricProcessInstanceQuery()
                .processDefinitionKey(Constant.PD_KEY_ZERO_DEFECT)
                .finished().involvedUser(user.getId())
                .variableValueEquals("assignee", user.getId())
                .orderByProcessInstanceEndTime().desc();
        List<HistoricProcessInstance> historicInstanceList = hisQuery.listPage(first, size);
        List<Defect> defectList = new ArrayList<>();
        for (HistoricProcessInstance instance : historicInstanceList) {
            Defect defect = new Defect();
            List<HistoricVariableInstance> varList = historyService.createHistoricVariableInstanceQuery()
                    .processInstanceId(instance.getId()).list();
            ActivitiUtil.setHistoricVars(defect, varList);
            defectList.add(defect);
        }
        return RestData.newData(defectList, first, size, hisQuery.count());
    }

    public RestResponse listDefect(String proCode, int first, int size) {
        HistoricProcessInstanceQuery hisProInsQuery = historyService.createHistoricProcessInstanceQuery()
                .processDefinitionKey(Constant.PD_KEY_ZERO_DEFECT)
                .variableValueEquals("projectId", proCode)
                .orderByProcessInstanceEndTime().desc();
        List<HistoricProcessInstance> hisProInsList = hisProInsQuery.listPage(first, size);
        List<Defect> defectList = new ArrayList<>();
        for (HistoricProcessInstance instance : hisProInsList) {
            Defect defect = new Defect();
            List<HistoricVariableInstance> varList = historyService.createHistoricVariableInstanceQuery()
                    .processInstanceId(instance.getId()).list();
            defect.setFinished(instance.getEndTime() != null);
            ActivitiUtil.setHistoricVars(defect, varList);
            defectList.add(defect);
        }
        return RestData.newData(defectList, first, size, hisProInsQuery.count());
    }
}
